<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>零元购</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <style>
        html,
        body {
            background: #ff7457;
        }

        header {
            padding: 0 15px;
            height: 44px;
            line-height: 44px;
            text-align: center;
            background-color: #ffffff;
            font-size: 16px;
            position: fixed;
            top: 0;
            right: 0;
            left: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            z-index: 10000;
        }

        header img {
            position: absolute;
            left: 15px;
            width: 22px;
            height: 22px;
        }

        .cont_head {
            width: 100%;
            position: relative;
        }

        .cont_head>img {
            display: block;
            width: 100%;
        }

        .cont_head .rule {
            font-size: 14px;
            color: #ffffff;
            line-height: 12px;
            position: absolute;
            left: 15%;
            bottom: 35px;
            z-index: 1000;
        }

        .cont_head .rule img {
            margin-left: 10px;
            height: 11px;
        }

        .cont_cont {
            position: relative;
            top: -15px;
            background-image: linear-gradient(0deg, #ff7457 0%, #00033c 100%);
            border-radius: 15px 15px 0 0;
        }

        .cont_cont_head {
            padding: 30px 15px 15px;
            color: #ffffff;
            text-align: center;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .cont_cont_head img {
            display: block;
            width: 30px;
            height: 30px;
            margin: 0 auto;
        }

        .cont_cont_head span {
            font-size: 12px;
            margin-top: 10px;
        }

        .cont_cont_head .dian {
            font-size: 30px;
            margin-bottom: 40px;
        }

        .cont_cont_head .dianleft {
            margin-left: 8px;
            margin-right: 20px;
        }

        .cont_cont_head .dianright {
            margin-left: 20px;
            margin-right: 8px;
        }

        .cont_cont_list {
            padding: 15px;
        }

        .cont_cont_list li {
            display: flex;
            padding: 15px;
            margin-bottom: 15px;
            background: #ffffff;
            border-radius: 10px;
        }

        li .li_cont {
            flex: auto;
        }

        li>img {
            flex: none;
            width: 2.5rem;
            height: 2.5rem;
            border-radius: 10px;
            margin-right: 15px;
        }

        li .title {
            margin-top: 5px;
            line-height: 17px;
            font-size: 13px;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
        }

        li .title .tm {
            color: #ffffff;
            line-height: 13px;
            background: #ff0c3f;
            border-radius: 2px;
            padding: 1px 2px;
            margin-right: 3px;
        }

        li .title .tb {
            color: #ffffff;
            line-height: 13px;
            background-image: linear-gradient(90deg, #ff8f00 0%, #ff5300 100%);
            border-radius: 2px;
            padding: 1px 2px;
            margin-right: 3px;
        }

        li .progress {
            margin: 10px 0 15px 0;
            width: 100%;
            height: 12px;
            background: #ffe5dd;
            border-radius: 6px;
        }

        li .progress_num {
            background-image: linear-gradient(45deg,
                    #ff3a24 0%,
                    #ff5340 100%);
            font-size: 11px;
            min-width: 6.5em;
            color: #ffffff;
            line-height: 12px;
            height: 12px;
            text-align: center;
            border-radius: 6px;
        }

        li .price {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        li .price img {
            width: 40px;
            height: 40px;
        }

        li .price_left :first-child span {
            text-decoration: line-through;
            font-size: 13px;
            color: #999999;
        }

        li .price_left :last-child {
            font-size: 15px;
            color: #f41e1e;
        }
    </style>
</head>

<body>
    <header class="header">
        <img src="../image/icon_return.png" alt="" onclick="back()">新人见面礼
    </header>
    <div class="content">
        <div class="cont_head"><img src="../image/0yuan_banner.png" alt="">
            <div class="rule" onclick="explain()">使用规则<img src="../image/0yuan_icon_more.png" alt=""></div>
        </div>
        <div class="cont_cont">
            <div class="cont_cont_head">
                <div><img src="../image/0yuan_icon_select.png" alt=""><span>选择心仪商品</span></div>
                <div class="dian dianleft">...</div>
                <div><img src="../image/0yuan_icon_buy.png" alt=""><span>支付购买</span></div>
                <div class="dian dianright">...</div>
                <div><img src="../image/0yuan_icon_subsidy.png" alt=""><span>收货全额补贴</span></div>
            </div>
            <ul class="cont_cont_list">
                <!-- <li>
                    <img src="../image/CB142864-F06E-4631-A442-A6D3CF751D2D.png" alt="">
                    <div class="li_cont">
                        <div class="title">
                            <span class="tm">天猫</span>
                            旗舰店旗舰旗舰店旗舰店旗舰店旗舰店旗舰店旗舰店旗舰店旗舰店旗舰店旗舰店旗舰店旗舰店店旗舰店旗舰店
                        </div>
                        <div class="progress">
                            <div class="progress_num"></div>
                        </div>
                        <div class="price">
                            <div class="price_left">
                                <div class="">券后价：<span>19.22</span></div>
                                <div class=""> 补 贴 ¥ <span>19.26</span></div>
                            </div>
                            <img src="../image/0yuan_grab.png" alt="">
                        </div>
                    </div>
                </li> -->
            </ul>
        </div>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript">
    let next_p = 1;
    let good_list = [];
    let aliBC;
    let item;
    apiready = function () {
        resume("zero");

        $api.dom('.content').style.marginTop = $api.fixStatusBar($api.dom('header')) + 'px';
        aliBC = api.require('aliBC');

        api.addEventListener({
            name: 'keyback'
        }, function (ret, err) {
            var frames = api.frames();
            if (frames.findIndex(item => {
                return item.name == "explain"
            }) != -1) {
                api.closeFrame({
                    name: "explain"
                });
            } else {
                api.closeWin({
                });
            }
        });

        addEvent();
        get_list();
    };

    function addEvent() {
        api.addEventListener({
            name: 'scrolltobottom',
            extra: {
                threshold: 1000,
            },
        }, function (ret, err) {
            get_list();
            api.removeEventListener({
                name: 'scrolltobottom'
            });
            setTimeout(function () {
                addEvent()
            }, 1000)
        });
    }

    function get_list() {
        $api.ajax({
            url: 'api/goods/list',
            method: 'get',
            data: {
                values: {
                    next_p: next_p,
                    type: 3,
                    is_zero: 1
                }
            },
            page: next_p,
            timeOutHtml: $api.dom(".cont_cont_list")
        }, function (ret) {
            if (ret.data.list.length == 0) {
                $api.html($api.dom(".cont_cont_list"), `<div style="text-align: center; color:#ffffff">暂无0元购商品，请稍后再试</div>`);
            } else {
                next_p = ret.data.next_p;
                good_list = good_list.concat(ret.data.list)
                ret.data.list.forEach(item => {
                    let html = `<li onclick="token(` + item.item_id + `)">
                     <img src="` + item.pict_url +
                        `_310x310.jpg" alt="" onerror="showDefaultImg(this)">
                    <div class="li_cont">
                        <div class="title">
                            <span class="` +
                        (item.user_type ? "tm" : "tb") +
                        `">` +
                        (item.user_type ? "天猫" : "淘宝") +
                        `</span>
                             ` + item.short_title +
                        `
                        </div>
                        <div class="progress">
                            <div class="progress_num" style="width:`+ item.volume.toString().slice(0, 2) + `%">已抢` + item.volume + `件</div>
                        </div>
                        <div class="price">
                            <div class="price_left">
                                <div class="">券后价：<span>` + item.end_price +
                        `</span></div>
                                <div class=""> 补 贴 ¥ <span>` + item.zk_final_price + `</span></div>
                            </div>
                            <img src="../image/0yuan_grab.png" alt="" >
                        </div>
                    </div>
                </li>`
                    $api.append($api.dom(".cont_cont_list"), html)
                });
            }
        });
    }

    function token(data) {
        item = good_list[good_list.findIndex(item => {
            return item.item_id == data
        })];

        if (getToken()) {
            relation(function (a) { purchase(a); },
                function () {
                    showLogin(function () { tbsq(); },
                        function () {
                            api.toast({
                                msg: '淘宝授权失败，请重试',
                                duration: 2000,
                                location: 'middle'
                            });
                        })
                })
        }
    }

    function relation(a, b) {
        $api.ajax({
            url: 'api/member/info',
            method: 'get',
        }, function (ret) {
            if (ret.data.info.relation_id > 0) {
                a && a(ret.data.info.relation_id);
            } else {
                b && b()
            }
        });
    }

    function showLogin(a, b) {
        aliBC.logout(function (ret, err) {
            if (ret.status) {
                aliBC.showLogin(function (ret, err) {
                    if (ret) {
                        a && a();
                    } else {
                        b && b();
                    }
                });
            }
        });
    }

    function tbsq() {
        $api.ajax({
            url: "api/member/tb_relate",
            method: 'get',
        }, function (ret) {
            aliBC.showPageByUrl({
                url: ret.data.oauth_url,
            }, function (ret, err) {
                if (ret) {
                    api.toast({
                        msg: '淘宝授权成功',
                        duration: 2000,
                        location: 'middle'
                    });
                } else {
                    api.toast({
                        msg: '您已取消淘宝授权',
                        duration: 2000,
                        location: 'middle'
                    });
                }
            });
        })
    }

    function purchase(a) {
        $api.ajax({
            url: "api/member/get_zero",
            method: 'post',
            data: {
                values: {
                    item_id: item.item_id
                }
            },
        }, function (ret) {
            aliBC.showPageByUrl({
                url: item.coupon_share_url + a,
                backUrl: api.systemType == "ios" ? "" : "tbopen24627965"
            }, function (ret, err) {
                if (!ret) {
                    api.toast({
                        msg: '淘宝页面打开失败，请重试',
                        duration: 2000,
                        location: 'middle'
                    });
                }
            });
        })
    }

    function explain() {
        api.openFrame({
            reload: true,
            name: 'explain',
            url: './explain.html',
            bgColor: "rgba(0,0,0,0.2)",
            animation: {
                type: "none",
            },
            pageParam: {
                type: 0
            }
        });
    }

    function back() {
        api.closeWin({
        });
    }
</script>

</html>